package com.zhong.gang.app.utils;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.zhong.gang.app.R;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.databinding.LayoutWaidiaoPaywayBinding;
import com.zhong.gang.app.db.UserBean;
import com.zhong.gang.app.db.UserUtils;
import com.zhong.gang.applib.CheseListener;
import com.zhong.gang.applib.ui.ZhangKouView;
import com.zhong.gang.applib.ui.chese.MaterialAct;
import com.zhong.gang.applib.ui.chese.MaterialTypeBean;
import com.zhong.gang.applib.utils.NumberUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Locale;

public class WaiDiaoPayWayView extends FrameLayout implements RadioGroup.OnCheckedChangeListener
        , TextWatcher, CheseListener, ShouFaView.MonenyChangedListener,
        ZhangKouView.ZhangKouListener {
    private LayoutWaidiaoPaywayBinding binding;
    private int status = 1;
    private int steelMillId = -1;
    private MaterialTypeBean bean;

    public WaiDiaoPayWayView(@NonNull Context context) {
        super(context);
        initView();
    }

    public WaiDiaoPayWayView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public WaiDiaoPayWayView(@NonNull Context context, @Nullable AttributeSet attrs,
            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_waidiao_payway, null);
        binding = DataBindingUtil.bind(view);
        binding.zhangkou.setListener(this);
        binding.setView(this);
        binding.shoufaLayout.setListener(this);
        binding.payRadio.setOnCheckedChangeListener(this);
        binding.shoufaLayout.setText(binding.remove);
        binding.mWeight.addTextChangedListener(this);
        binding.pWeight.addTextChangedListener(this);
        binding.kWeight.addTextChangedListener(this);
        binding.zhangkou.setCheckStatus(false);
        show(false);
        ((RadioButton) binding.payRadio.getChildAt(0)).setChecked(true);

        onCheckedChanged(binding.payRadio, 0);
        this.addView(view);
    }

    public void setSteelMillId(int steelMillId, String address, double money) {
        this.steelMillId = steelMillId;
        for (int n = 0; n < binding.payRadio.getChildCount(); n++) {
            View view = binding.payRadio.getChildAt(n);
            if (view instanceof RadioButton && ((RadioButton) view).isChecked()) {
                binding.shoufaLayout.setData(steelMillId, 1, money);
            }
        }
        binding.addressTv.setText(address);
        onCheseBack(null);
    }

    public int getType() {
        for (int n = 0; n < binding.payRadio.getChildCount(); n++) {
            View view = binding.payRadio.getChildAt(n);
            if (view instanceof RadioButton && ((RadioButton) view).isChecked()) {
                return n;
            }
        }
        return 0;
    }

    /**
     * 获取毛重
     */
    public double getMWeight() {
        return getNumber(binding.mWeight);
    }

    /**
     * 获取皮重
     *
     * @return
     */
    public double getPWeight() {
        return getNumber(binding.pWeight);
    }

    /**
     * 获取扣渣
     *
     * @return
     */
    public double getKWeight() {
        return getNumber(binding.kWeight);
    }

    /**
     * 获取电话
     *
     * @return
     */
    public double getWeight() {
        return getNumber(binding.jWeight);
    }

    /**
     * 获取文字类型
     *
     * @param textView
     * @return
     */
    private double getNumber(TextView textView) {
        return NumberUtils.getNumber(textView.getText().toString());
    }


    public String getJsonArray() {
        for (int n = 0; n < binding.payRadio.getChildCount(); n++) {
            RadioButton radioButton = (RadioButton) binding.payRadio.getChildAt(n);
            if (radioButton.isChecked()) {
                if (n == 0) {
                    return binding.shoufaLayout.getJsonArray();
                }
            }
        }
        return getThisJsonArray();
    }

    public String getThisJsonArray() {
        if (bean == null)
            return null;
        JSONArray array = new JSONArray();
        try {
            JSONObject object = new JSONObject();
            object.put("materialTypeId", bean.getId());
            object.put("price", 0 + "");
            object.put("weight", 0 + "");
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return array.toString();
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int n = 0; n < group.getChildCount(); n++) {
            View view = group.getChildAt(n);
            if (view instanceof RadioButton && ((RadioButton) view).isChecked()) {
                binding.kWeight.setText("");
                show(n == 1);
                binding.zhangkouMonenyLine.setVisibility(n == 0 ? VISIBLE : GONE);
                binding.shoufaLayout.setData(binding.shoufaLayout.getSteelMillId(), 1,
                        binding.shoufaLayout.getMoney());
            }
        }
    }

    public int getStatus() {
        return status;
    }

    public double getUpMoneny() {
        return binding.zhangkou.getMoneny();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.material_layout:
                if (steelMillId > 0) {
                    UserBean userBean = UserUtils.getIntent(getContext()).getUser();
                    MaterialAct.openAct(getContext(), Api.materialTypeList,
                            steelMillId + "", 1 + ""
                            , userBean.isLogin(), userBean.getToken(), userBean.getId() + ""
                            , UserUtils.getIntent(getContext()).getRoleId() + "", this);
                }
                break;
        }
    }

    //送货地点     装货料型 毛重 皮重 净重
    //打款放车     毛重 皮重 扣渣  装货廖鑫1  廖鑫2 结算金额
    public void show(boolean showStatus) {
        if (showStatus) {
            binding.addressLayout.setVisibility(VISIBLE);
            binding.addressLine.setVisibility(VISIBLE);
            binding.materialLayout.setVisibility(VISIBLE);
            binding.materialLine.setVisibility(VISIBLE);

            binding.kWeightLayout.setVisibility(GONE);
            binding.shoufaLayout.setVisibility(GONE);
            binding.statusLayout.setVisibility(GONE);
            binding.zhangkou.setVisibility(GONE);
            binding.monenyLayout.setVisibility(GONE);
        } else {
            binding.addressLayout.setVisibility(GONE);
            binding.addressLine.setVisibility(GONE);
            binding.materialLayout.setVisibility(GONE);
            binding.materialLine.setVisibility(GONE);

            binding.kWeightLayout.setVisibility(VISIBLE);
            binding.shoufaLayout.setVisibility(VISIBLE);
            binding.statusLayout.setVisibility(VISIBLE);
            binding.zhangkou.setVisibility(VISIBLE);
            binding.monenyLayout.setVisibility(VISIBLE);
        }
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        double jWeight = getMWeight() - getPWeight() - getKWeight();
        binding.jWeight.setText(String.format(Locale.CHINA, "%.2f", (float) jWeight));
        binding.shoufaLayout.setAllWeight(jWeight);
    }

    @Override
    public void onCheseBack(MaterialTypeBean bean) {
        this.bean = bean;
        if (bean == null) {
            binding.materialTv.setText("");
            return;
        }
        binding.materialTv.setText(bean.getName());
    }

    @Override
    public void afterMonenyChanged(double allMoneny) {
        binding.zhangkou.setMoneny(allMoneny);
    }

    @Override
    public void onZhangKou(double money) {
        binding.moneny.setText(money + "");
    }
}
